const getObjType = (obj) => {
    if (typeof obj == 'object') {
        if (obj instanceof Array) {
            return 'array';
        } else {
            return 'object';
        }
    } else {
        return 'value';
    }
};
/**
 * 对象合并深拷贝
 * @param obj1 空对象或数组
 * @param obj2 要拷贝的对象
 * @returns
 */
export const deepExtend = (obj1, obj2) => {
    const obj1Type = getObjType(obj1);
    const obj2Type = getObjType(obj2);
    //如果传入的两个参数类型不一致，则先设置数据类型
    if (obj1Type != obj2Type) {
        if (obj2Type == 'object') {
            obj1 = obj2 ? {} : null;
        } else if (obj2Type == 'array') {
            obj1 = [];
        } else {
            obj1 = obj2;
            return obj1;
        }
    }
    if (obj2Type == 'object') {
        for (const prop in obj2) {
            if (!obj1[prop]) {
                obj1[prop] = obj2[prop] ? {} : null;
            }
            obj1[prop] = deepExtend(obj1[prop], obj2[prop]);
        }
    } else if (obj2Type == 'array') {
        obj2 = obj2.slice();
        const newObj = obj2 instanceof Array ? [] : {};
        for (const item in obj2) {
            //只复制元素自身的属性，不复制原型链上的
            if (Object.prototype.hasOwnProperty.call(obj2, item)) {
                newObj[item] = typeof obj2[item] == 'object' ? deepExtend([], obj2[item]) : obj2[item]; //判断属性值类型
            }
        }
        obj1 = obj1.concat(newObj);
    } else {
        obj1 = obj2;
    }
    return obj1;
};

export default {
    deepExtend,
};
